var app = getApp(), dataApi = require("../../provider/dataApi.js");

require("../../provider/pipe.js");

Page({
    data: {
        statusBarHeight: "",
        titleBarHeight: "",
        windowHeight: 0,
        knowledgeList: [],
        knowledgeIndex: 0,
        payOpen: "",
        title: "",
        totalNum: "0",
        item: {},
        isLoading: !0,
        isBool: !0,
        show: !1,
        activationShow: !1,
        setCDKey: "",
        fItem: {},
        userInfo: {},
        isIos: !1,
        params: {}
    },
    getCode: function() {
        this.setData({
            show: !1,
            activationShow: !0
        });
    },
    onClose: function() {
        this.setData({
            activationShow: !1
        });
    },
    onChange: function(event) {
        this.setData({
            setCDKey: event.detail
        });
    },
    onConfirm: function() {
        this.data.setCDKey ? dataApi.setCDKey({
            uid: wx.getStorageSync("uid"),
            code: this.data.setCDKey
        }).then(function(response) {
            console.log(response), wx.showToast({
                icon: "none",
                title: response.message
            });
        }).catch(function(error) {
            console.log(error), wx.showToast({
                icon: "none",
                title: "该激活码不可用"
            });
        }) : wx.showToast({
            icon: "none",
            title: "请输入激活码"
        });
    },
    onLoad: function(options) {
        if (wx.getStorageSync("uid")) {
            1 == options.type && this.setData({
                params: options
            }), console.log(this.data.params);
            var deviceModel = wx.getSystemInfoSync().model.slice(0, 6), deviceType = wx.getSystemInfoSync().model.slice(0, 4);
            options = !1, "iPhone" != deviceModel && "iPad" != deviceType || (options = !0), deviceModel = wx.getSystemInfoSync().windowHeight, 
            this.setData({
                statusBarHeight: app.globalData.statusBarHeight,
                titleBarHeight: app.globalData.titleBarHeight,
                windowHeight: deviceModel,
                title: app.globalData.title,
                isIos: options
            });
        } else wx.navigateTo({
            url: "/pages/login/login"
        });
    },
    goBack: function() {
        wx.navigateBack({
            delta: 1
        });
    },
    onReady: function() {},
    ltChoose: function(event) {
        this.setData({
            knowledgeIndex: event.currentTarget.dataset.i
        });
    },
    rtChoose: function(event) {
        var chapterId = event.currentTarget.dataset.chapterid, fItem = event.currentTarget.dataset.fitem;
        console.log(event), (event = event.currentTarget.dataset.oitem).chapterId = chapterId, this.setData({
            totalNum: event.total_num,
            item: event,
            fItem: fItem
        }), console.log(fItem), 1 != fItem.is_can ? this.data.isIos && 2 == this.data.userInfo.IOS ? this.iosTip() : this.setData({
            show: !0
        }) : (app.globalData.id = event.id, wx.navigateTo({
            url: "../practice/practice"
        }));
    },
    iosTip: function() {
        wx.showModal({
            cancelText: "关闭",
            confirmText: "联系客服",
            content: "由于相关规范，ios功能暂不可用，如有问题请联系客服",
            showCancel: !0,
            title: "提示",
            success: function(response) {
                response.confirm && wx.navigateTo({
                    url: "/pages/contactService/contactService"
                });
            }
        });
    },
    onShow: function() {
        this.getUserInfo(), this.getSequence();
    },
    getSequence: function() {
        var self = this;
        app.globalData.op = "special", dataApi.sequence({
            uid: wx.getStorageSync("uid"),
            op: "getallspecial"
        }).then(function(response) {
            console.log(response);
            var hasData = !1;
            0 < response.data.list.length && (hasData = !0), self.setData({
                knowledgeList: response.data.list,
                isLoading: !1,
                payOpen: response.data.pay_open,
                freenum: response.data.freenum,
                isBool: hasData
            }), self.firstNavTabs();
        }).catch(function(error) {
            console.log(error);
        });
    },
    firstNavTabs: function() {
        var params = this.data.params;
        if (params && 1 == params.type) {
            for (var knowledgeList = this.data.knowledgeList, index = 0, i = 0; i < knowledgeList.length; i++) if (params.id == knowledgeList[i].id) {
                index = i;
                break;
            }
            this.setData({
                knowledgeIndex: index
            });
        }
    },
    onHide: function() {},
    tryAnswer: function(event) {
        console.log(event), event = event.currentTarget.dataset.num, app.globalData.id = this.data.item.id, 
        wx.navigateTo({
            url: "../practice/practice?num=" + event
        });
    },
    getUserInfo: function() {
        var self = this;
        dataApi.userinfo({
            uid: wx.getStorageSync("uid"),
            op: "getinfo"
        }).then(function(response) {
            console.log(response), self.setData({
                userInfo: response.data.info
            });
        }).catch(function(error) {
            console.log(error);
        });
    },
    closePayPopup: function() {
        this.setData({
            show: !1
        });
    },
    goVipBtn: function() {
        wx.navigateTo({
            url: "../vip/vip"
        }), this.setData({
            show: !1
        });
    },
    goPay: function() {
        var self = this;
        dataApi.wxPay({
            uid: wx.getStorageSync("uid"),
            id: this.data.item.chapterId,
            type: "1"
        }).then(function(response) {
            console.log(response), wx.requestPayment({
                timeStamp: String(response.data.timeStamp),
                nonceStr: response.data.nonceStr,
                package: response.data.package,
                signType: response.data.signType,
                paySign: response.data.paySign,
                success: function(response) {
                    wx.showToast({
                        icon: "success",
                        title: "支付成功"
                    }), self.setData({
                        show: !1
                    }), self.getSequence();
                },
                fail: function(error) {
                    wx.showToast({
                        icon: "none",
                        title: "支付失败,请重试~"
                    });
                }
            });
        }).catch(function(error) {
            console.log(error);
        });
    },
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});